{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n",
    "#\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#     http://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "# Preamble to mimick book environment\n",
    "import logging\n",
    "from pprint import pprint\n",
    "from sys import stdout as STDOUT"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Use optional\tpositional\targuments\t(often\tcalled\tstar\targ)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "My numbers are: 1, 2\n",
      "Hi there\n"
     ]
    }
   ],
   "source": [
    "# Example 1\n",
    "def log(message, values):\n",
    "    if not values:\n",
    "        print(message)\n",
    "    else:\n",
    "        values_str = ', '.join(str(x) for x in values)\n",
    "        print('%s: %s' % (message, values_str))\n",
    "\n",
    "log('My numbers are', [1, 2])\n",
    "log('Hi there', [])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Better!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "My numbers are: 1, 2\n",
      "Hi there\n"
     ]
    }
   ],
   "source": [
    "# Example 2\n",
    "def log(message, *values):  # The only difference\n",
    "    if not values:\n",
    "        print(message)\n",
    "    else:\n",
    "        values_str = ', '.join(str(x) for x in values)\n",
    "        print('%s: %s' % (message, values_str))\n",
    "\n",
    "log('My numbers are', 1, 2)\n",
    "log('Hi there')  # Much better"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Favorite colors: 7, 33, 99\n"
     ]
    }
   ],
   "source": [
    "# Example 3\n",
    "favorites = [7, 33, 99]\n",
    "log('Favorite colors', *favorites)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)\n"
     ]
    }
   ],
   "source": [
    "# Example 4\n",
    "def my_generator():\n",
    "    for i in range(10):\n",
    "        yield i\n",
    "\n",
    "def my_func(*args):\n",
    "    print(args)\n",
    "\n",
    "it = my_generator()\n",
    "my_func(*it)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1: Favorites: 7, 33\n",
      "Favorite numbers: 7: 33\n"
     ]
    }
   ],
   "source": [
    "# Example 5\n",
    "def log(sequence, message, *values):\n",
    "    if not values:\n",
    "        print('%s: %s' % (sequence, message))\n",
    "    else:\n",
    "        values_str = ', '.join(str(x) for x in values)\n",
    "        print('%s: %s: %s' % (sequence, message, values_str))\n",
    "\n",
    "log(1, 'Favorites', 7, 33)      # New usage is OK\n",
    "log('Favorite numbers', 7, 33)  # Old usage breaks"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
